package comp
{
	import comp.Interface.IDispose;
	import comp.style.css.CssAchieve;
	import comp.style.skin.*;
	import comp.style.theme.ScrollBarTheme;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * <h1>滚动条</h1>
	 * <h2>指定maxHeight=滚动目标的最大高度,height为滚动目标的可视高度,当滚动时onScroll(Y:Number)会自动计算出目标的滚动距离</h2>
	 * 支持换肤</br>
	 * 样式:topSkin上箭头默认,topOverSkin上箭头划过,topDownSkin上箭头按下,
	 * bottomSkin,bottomOverSkin,bottomDownSkin,
	 * middleSkin,middleOVerSkin,middleDownSkin.</br>
	 * 背景:backgroundImage,backgroundColor,backgroundRepeat(false|true);
	 * </br>
	 * 
	 * 示例comp.Panel 233
	 * @author:pu
	 * data:2010.9.28
	 */
	public class ScrollBar extends UIComponent
	{
		include "style/BorderStyles";

		//*************************
		//样式
		//*************************
		private var _topSkin:Class;//上箭头默认
		private var _topOverSkin:Class;//上箭头划过
		private var _topDownSkin:Class;//上箭头按下

		private var _bottomSkin:Class;//下箭头默认
		private var _bottomOverSkin:Class;//下箭头划过
		private var _bottomDownSkin:Class;//下箭头按上
		
		private var _middleSkin:Class;///拉动条默认
		private var _middleOVerSkin:Class;//拉动条划过
		private var _middleDownSkin:Class;//拉动条按下
		
		private var _autoSize:Boolean;//中间滚动条自动增加
		
		public function ScrollBar()
		{
			super();
			
			addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			_currentSkin=new Object;//当前皮肤
			
			this._themeName="comp::ScrollBar";
		}

		public function get scrollY():Number
		{
//			if(_scrollY==0 && _barBtn && _upBtn)
//				_barBtn.y-_upBtn.height;
			return _scrollY;
		}

		public function set scrollY(value:Number):void
		{
			_scrollY = value;
		}

		override public function dispose():void
		{
			super.dispose();
			_topSkin=null;
			_topOverSkin=null;
			_topDownSkin=null;
			_bottomSkin=null;
			_bottomOverSkin=null;
			_bottomDownSkin=null;
			_middleSkin=null;
			_middleOVerSkin=null;
			_middleDownSkin=null;
			
			_currentSkin=null;//当前皮肤

			removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			if(this.stage)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onDrage);
			}
			
			for(var i:uint=numChildren;i>0;i--)
			{
				var dis:DisplayObject=getChildAt(0);
				if(dis is IDispose)
					(dis as IDispose).dispose();
				dis=null;
				removeChildAt(0);
			}
			
			if(parent)
				parent.removeChild(this);
		}

		/**
		 * Scollbar滚动时触发的函数：
		 * 格式function(y:Number);
		 */
		public function set onScroll(value:Function):void
		{
			_scrollFunction = value;
		}

		public function get middleDownSkin():Class
		{
			return _middleDownSkin;
		}

		public function set middleDownSkin(value:Class):void
		{
			_middleDownSkin = value;
		}

		public function get middleOverSkin():Class
		{
			return _middleOVerSkin;
		}

		public function set middleOverSkin(value:Class):void
		{
			_middleOVerSkin = value;
		}

		public function get middleSkin():Class
		{
			return _middleSkin;
		}

		public function set middleSkin(value:Class):void
		{
			_middleSkin = value;
		}

		public function get bottomDownSkin():Class
		{
			return _bottomDownSkin;
		}

		public function set bottomDownSkin(value:Class):void
		{
			_bottomDownSkin = value;
		}

		public function get bottomOverSkin():Class
		{
			return _bottomOverSkin;
		}

		public function set bottomOverSkin(value:Class):void
		{
			_bottomOverSkin = value;
		}

		public function get bottomSkin():Class
		{
			return _bottomSkin;
		}

		public function set bottomSkin(value:Class):void
		{
			_bottomSkin = value;
		}

		public function get topDownSkin():Class
		{
			return _topDownSkin;
		}

		public function set topDownSkin(value:Class):void
		{
			_topDownSkin = value;
		}

		public function get topOverSkin():Class
		{
			return _topOverSkin;
		}

		public function set topOverSkin(value:Class):void
		{
			_topOverSkin = value;
		}

		public function get topSkin():Class
		{
			return _topSkin;
		}

		public function set topSkin(value:Class):void
		{
			_topSkin = value;
		}
		
		/**
		 * 对应的滚动对像大小改变
		 */
		public function onSizeChange():void
		{
			commitStyles();
		}

		//protected
		
		protected function rollOverHandler(e:MouseEvent):void
		{
			_state="OverSkin";
			if(e.target is Sprite && e.target!=this)
			{
				//if(e.target.name!="middle")
					viewSkin(e.target,_state);
			}
		}
		
		protected function rollOutHandler(e:MouseEvent):void
		{
//			_state="Skin";
//			if(e.target is Sprite && e.target!=this)
//				viewSkin(e.target,_state);
			//stopMiddleDrag();
		}
		
		protected function mouseDownHandler(e:MouseEvent):void
		{
			_state="DownSkin";
			if(e.target is Sprite && e.target!=this)
			{
				viewSkin(e.target,_state);
				if(e.target.name=="middle")
				{
					_isDrage=true;
					_clickY=e.stageY;//点击点Y座标
					this.stage.addEventListener(MouseEvent.MOUSE_MOVE,onDrage);
					this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
					trace("注册事件:middle"+_clickY+onDrage+mouseUpHandler);
				}else if(e.target.name=="top")
					scroll(-20*height/maxHeight);
				else if(e.target.name=="bottom")
					scroll(20*height/maxHeight);
			}
		}
		
		protected function mouseUpHandler(e:MouseEvent):void
		{
			_state="Skin";
			stopMiddleDrag();
			//if(e.target is Sprite && e.target!=this)
				viewSkin(_barBtn,_state);
		}
		
		/**
		 * 停止中单滚动条滚动
		 */
		protected function stopMiddleDrag():void
		{
			_isDrage=false;
			if(stage)
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onDrage);
		}
		
		/**
		 * 创建显示容器
		 */
	  	override protected function createChildren():void
		{
			_upBtn=new Sprite;
			_downBtn=new Sprite;
			_barBtn=new Sprite;

			_upBtn.name="top";
			_downBtn.name="bottom";
			_barBtn.name="middle";
			addChild(_upBtn);
			addChild(_barBtn);
			addChild(_downBtn);
		}
		
		/**
		 * 显示皮肤
		 */
		protected function viewSkin(obj:Object,state:String):void
		{
			var s:Class;//皮肤
			var n:String=obj.name;			
			var newSkin:DisplayObject;
			
			s=getSkin(n,state);//得到当前皮肤
			
			var c:DisplayObject=obj.getChildByName(state);//找新皮肤
			
			//稳藏当前皮肤 
			if(n in _currentSkin && _currentSkin[n])
				DisplayObject(_currentSkin[n]).visible=false;
			
			if(c)//新皮肤存在
				_currentSkin[n]=c;
			else if(s)
			{
				newSkin=new s;
				newSkin.name=state;//状态名
				obj.addChild(newSkin);
				obj.mouseChildren=false;
				_currentSkin[n]=newSkin;
			}
		
			if(_currentSkin[n])
				_currentSkin[n].visible=true;
			
		}
		
		protected function getSkin(objName:String,skinName:String):Class
		{
			var s:Class=getStyle(objName+skinName);
			if(!s)//如果没有
				s=getStyle(objName+"Skin");//默认皮肤
			
			return s;
		}
		
		/**
		 * 托动的时候发生
		 */
		protected function onDrage(e:MouseEvent):void
		{
			if(_isDrage)
			{
				var dy:Number=e.stageY-_clickY;
				_clickY=e.stageY;
				scroll(dy);
			}
		}

		/**
		 * 滚动的时候发生
		 */
		public function scroll(num:Number):void
		{
			var y:Number=_barBtn.y+num;//滚动距离
			var truey:Number=height-_downBtn.height-_barBtn.height;//滚动高度
			if(y<_upBtn.height)
			{
				y=_upBtn.height;
				num=0;
			}
			if(y>truey)
			{
				y=truey;
				num=0;
			}
				
			_barBtn.y=y;//滚动
			
			_scrollY=_barBtn.y-_upBtn.height;
				
			if(_scrollFunction!=null)
			{
				if(this.getStyle("autoSize"))
					_scrollFunction(_scrollY*maxHeight/(height-_downBtn.height-_upBtn.height))//;目标对像滚动距离
				else
				{
					if(_scrollY>maxHeight)
						_scrollY=maxHeight;					
					_scrollFunction(_scrollY*(maxHeight-height)/(height-_downBtn.height-_barBtn.height -_upBtn.height))//;目标对像滚动距离
				}

			}
				
		}
		
		public function get autoSize():Boolean
		{
			return _autoSize;
		}
		
		public function set autoSize(value:Boolean):void
		{
			_autoSize = value;
		}
		
		/**
		 * 改变样式和皮肤
		 */
		override protected function commitStyles():void
		{
			if(this.numChildren<3)
				return;
			
			viewSkin(_upBtn,"Skin");
			viewSkin(_barBtn,"Skin");
			viewSkin(_downBtn,"Skin");
			_barBtn.y=_upBtn.height;
			if(this.getStyle("autoSize"))
				_barBtn.height=(height-_upBtn.height-_downBtn.height)*(height/maxHeight);
			
			_barBtn.x=(width-_barBtn.width)/2;
//			else
//				s1.height
			_downBtn.y=height-_downBtn.height;
			
			var c:CssAchieve
			CssAchieve.mergeBackGrounde(this);
		}

		/**
		 * 当前显示区域
		 */
		public function get currRect():Rectangle
		{
			return _currRect;
		}
		
		public function set currRect(value:Rectangle):void
		{
			_currRect = value;
		}
		
		/**
		 * 整个区域
		 */
		public function get wholeRect():Rectangle
		{
			return _wholeRect;
		}
		
		/**
		 * @private
		 */
		public function set wholeRect(value:Rectangle):void
		{
			_wholeRect = value;
		}
		
		/**
		 * 滚动目标
		 */
		public function get target():DisplayObject
		{
			return _target;
		}
		
		/**
		 * @private
		 */
		public function set target(value:DisplayObject):void
		{
			_target = value;
		}
		
		//private
		private var _scrollY:Number=0;
		private var _clickY:Number=0;//记录当前点击位置    
		private var _originalY:Number;//滚动条原始座标Y位置
		private var _scrollFunction:Function;
		private var _currRect:Rectangle;//滚动的矩形区域；
		private var _wholeRect:Rectangle;//整个区域
		private var _target:DisplayObject;//滚动目标
		
		//滚动时触发	
		private var _state:String;//鼠标状态
		private var _currentSkin:Object;//当前皮肤
		private var _isDrage:Boolean=false;//是否拖动

		private var _barBtn:Sprite;
		private var _upBtn:Sprite;
		private var _downBtn:Sprite;
		
	}
}